const path = require('path');
const CracoLessPlugin = require('craco-less');
module.exports = {
  webpack: {
    configure: (webpackConfig, { env, paths }) => {
      // 核心修复：彻底覆盖所有构建路径
      const newBuildPath = path.resolve(__dirname, 'chatBI');
      
      // 1. 修改构建输出目录
      paths.appBuild = newBuildPath;
      
      // 2. 强制覆盖 webpack 输出路径
      webpackConfig.output.path = newBuildPath;
      
      // 3. 处理 Service Worker 等插件的路径问题
      webpackConfig.plugins.forEach((plugin) => {
        if (plugin.constructor.name === 'InjectManifest') {
          plugin.config.maximumFileSizeToCacheInBytes = 10 * 1024 * 1024;
          plugin.config.dontCacheBustURLsMatching = /\.[0-9a-f]{8}\./;
          // 关键：修正 Service Worker 文件输出路径
          plugin.config.swDest = path.join(newBuildPath, 'service-worker.js');
        }
      });

      // 4. 保持你的其他配置
      webpackConfig.output.publicPath = process.env.NODE_ENV === 'production' ? '/chatBI/' : '/';
      webpackConfig.resolve.alias = {
        '@': path.resolve(__dirname, 'src/'),
      };

      return webpackConfig;
    },
  },
     plugins: [
    {
      plugin: CracoLessPlugin,
      options: {
        lessLoaderOptions: {
          lessOptions: {
            javascriptEnabled: true, // 允许在 Less 文件中使用 JavaScript 表达式
          },
        },
      },
    },
  ],
  devServer: {
    host: '0.0.0.0',
    proxy: {
      '/chatbiv3': {
        // target: 'http://10.217.247.48:9050', // 后端接口的域名 
        target: 'http://8.140.229.23:8576', // 后端接口的域名
        changeOrigin: true, // 允许跨域
        onProxyReq: (proxyReq, req, res) => {
          // 设置请求头，保持长连接
          proxyReq.setHeader('Connection', 'keep-alive');
        },
        onProxyRes: (proxyRes, req, res) => {
          // 禁用 Nginx 或其他服务器的响应缓冲
          proxyRes.headers['X-Accel-Buffering'] = 'no';
          // 禁用代理服务器的响应缓冲
          res.setHeader('Cache-Control', 'no-cache, no-transform');
          res.setHeader('Content-Type', 'text/event-stream'); // 如果是 SSE，设置正确的 Content-Type
        },
        // 禁用代理的响应缓冲
        proxyReqWriteStreamOptions: {
          buffering: false, // 确保代理不缓冲响应
        },
      },
    }
  }
};
